﻿#include "socket_async_event_args.h"


namespace nsshare
{

namespace sys
{

SocketAsyncEventArgs::SocketAsyncEventArgs()
    : AsyncEventArgs(), error_(0), transfered_(0),
      buffer_(0), count_(0), socket_flags_(0),
      accept_socket_(0), connect_socket_(0), 
      last_op_(SocketAsyncOp::kAsyncUnknow),
      completion_delegate_(), reuse_(false)
{
}

SocketAsyncEventArgs::SocketAsyncEventArgs(Event & e)
    : AsyncEventArgs(e), error_(0), transfered_(0),
      buffer_(0), count_(0), socket_flags_(0),
      accept_socket_(0), connect_socket_(0), 
      last_op_(SocketAsyncOp::kAsyncUnknow),
      completion_delegate_(), reuse_(false)
{
}

void SocketAsyncEventArgs::SetBuffer(const void * buffer, size_t size)
{
    buffer_ = const_cast<void *>(buffer);
    count_ = size;
}

void SocketAsyncEventArgs::SetBuffer(void * buffer, size_t size)
{
    buffer_ = buffer;
    count_ = size;
}

void * SocketAsyncEventArgs::buffer() const
{
    return buffer_;
}

size_t SocketAsyncEventArgs::count() const
{
    return count_;
}

uint32_t SocketAsyncEventArgs::error() const
{
    return error_;
}

uint32_t SocketAsyncEventArgs::transfered() const
{
    return transfered_;
}

uint32_t SocketAsyncEventArgs::socket_flags() const
{
    return socket_flags_;
}

SocketAsyncOp::Value SocketAsyncEventArgs::last_op() const
{
    return last_op_;
}

IPEndPoint SocketAsyncEventArgs::remote_endpoint() const
{
    return remote_endpoint_;
}

Socket * SocketAsyncEventArgs::accept_socket() const
{
    return accept_socket_;
}

Socket * SocketAsyncEventArgs::connect_socket() const
{
    return connect_socket_;
}

bool SocketAsyncEventArgs::reuse() const
{
    return reuse_;
}

void SocketAsyncEventArgs::set_accept_socket(Socket & socket)
{
    accept_socket_ = &socket;
}

void SocketAsyncEventArgs::set_remote_endpoint(const IPEndPoint & ep)
{
    remote_endpoint_ = ep;
}

void SocketAsyncEventArgs::set_reuse(bool value)
{
    reuse_ = value;
}

void SocketAsyncEventArgs::set_completion_delegate(SocketAsyncEventHandler * handler)//NOLINT
{
    completion_delegate_ = handler;
}

void SocketAsyncEventArgs::OnCompleted(uint32_t error, uint32_t transfered)
{
    error_ = error;
    transfered_ = transfered;
    completion_delegate_(this, *this);
}

}
}